package org.xc.x_ffmpeg;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());

//        FFMpegUtil.Info();

//        try {
////            separation();
//            merge();
//        } catch (FFmpegCommandAlreadyRunningException e) {
//            e.printStackTrace();
//        }
    }

    private void merge() throws FFmpegCommandAlreadyRunningException {
        String h264 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.h264";
        String aac = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.aac";
        String m9 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/9.mp4";
        File file = new File(m9);
        if (file.exists()) {
            file.delete();
        }

        String cmd = "-i "+aac+" -i "+h264+" " + m9;
        FFmpeg.getInstance(this).execute(cmd.split(" "), new FFmpegExecuteResponseHandler() {
            @Override
            public void onSuccess(String message) {
                Log.e("merge","onSuccess->" + message);
            }

            @Override
            public void onProgress(String message) {
                Log.e("merge","onProgress->" + message);
            }

            @Override
            public void onFailure(String message) {
                Log.e("merge","onFailure->" + message);
            }

            @Override
            public void onStart() {
                Log.e("merge","onStart");
            }

            @Override
            public void onFinish() {
                Log.e("merge","onFinish");
            }
        });
    }

    private void separation() throws FFmpegCommandAlreadyRunningException {
        String m1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.mp4";

        String h264 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.h264";
        File h264File = new File(h264);
        if (h264File.exists()) {
            h264File.delete();
        }

        String aac = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.aac";
        File aacFile = new File(aac);
        if (aacFile.exists()) {
            aacFile.delete();
        }

        //提取音频
        String cmd = "-i "+m1+" -acodec copy -vn " + aac;
        FFmpeg.getInstance(this).execute(cmd.split(" "), new FFmpegExecuteResponseHandler() {
            @Override
            public void onSuccess(String message) {
                Log.e("separation","onSuccess->" + message);
            }

            @Override
            public void onProgress(String message) {
                Log.e("separation","onProgress->" + message);
            }

            @Override
            public void onFailure(String message) {
                Log.e("separation","onFailure->" + message);
            }

            @Override
            public void onStart() {
                Log.e("separation","onStart");
            }

            @Override
            public void onFinish() {
                Log.e("separation","onFinish");
            }
        });

        String cmd1 = "-i "+m1+" -codec copy -bsf: h264_mp4toannexb -f h264 " + h264;
        FFmpeg.getInstance(this).execute(cmd1.split(" "), new FFmpegExecuteResponseHandler() {
            @Override
            public void onSuccess(String message) {
                Log.e("separation","onSuccess->" + message);
            }

            @Override
            public void onProgress(String message) {
                Log.e("separation","onProgress->" + message);
            }

            @Override
            public void onFailure(String message) {
                Log.e("separation","onFailure->" + message);
            }

            @Override
            public void onStart() {
                Log.e("separation","onStart");
            }

            @Override
            public void onFinish() {
                Log.e("separation","onFinish");
            }
        });
    }

    private void contact() {
        String m1 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/1.mp4";
        String m2 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/2.mp4";
        String m3 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/3.mp4";

        String m = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666" + "/m.mp4";
        File mfile = new File(m);
        if (mfile.exists()) {
            mfile.delete();
        }

        try {
            File fileMp4Txt = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  + "666/m.txt");
            if (fileMp4Txt.exists()) {
                fileMp4Txt.delete();
            }
            fileMp4Txt.createNewFile();

            FileOutputStream fos = new FileOutputStream(fileMp4Txt);
            String s;
            s = "file '" + m1+ "'";
            fos.write(s.getBytes());
            fos.write(System.getProperty("line.separator").getBytes());

            s = "file '" + m2+ "'";
            fos.write(s.getBytes());
            fos.write(System.getProperty("line.separator").getBytes());

            s = "file '" + m3+ "'";
            fos.write(s.getBytes());
            fos.write(System.getProperty("line.separator").getBytes());

            fos.flush();
            fos.close();

//            String cmd = "-i "+h264+" -i "+aac+" -vcodec copy -acodec copy " + c7path;
            String cmd1 = "-f concat -safe 0 -i "+fileMp4Txt.getAbsolutePath()+" -c copy " + m;
            FFmpeg.getInstance(this).execute(cmd1.split(" "), new FFmpegExecuteResponseHandler() {
                @Override
                public void onSuccess(String message) {
                    Log.e("execute","onSuccess->" + message);
                }

                @Override
                public void onProgress(String message) {
                    Log.e("execute","onProgress->" + message);
                }

                @Override
                public void onFailure(String message) {
                    Log.e("execute","onFailure->" + message);
                }

                @Override
                public void onStart() {
                    Log.e("execute","onStart");
                }

                @Override
                public void onFinish() {
                    Log.e("execute","onFinish");
                }
            });
//        } catch (FFmpegNotSupportedException e) {
//            e.printStackTrace();
        } catch (FFmpegCommandAlreadyRunningException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
